package net.wykj.ms.streaming.audio;

import java.io.IOException;

import net.wykj.ms.rtp.AACADTSPacketizer;
import net.wykj.ms.streaming.MediaStream;

import android.media.MediaRecorder;

/**
 * This will stream AMRNB from the mic over RTP
 * Just call setDestination(), prepare() & start()
 */
public class AACStream extends MediaStream {

	public static boolean aacSupported = true;
	
	public static void setAACSupported(boolean b) {
		aacSupported = b;
	}
	
	public AACStream() {
		super();
		
		AACADTSPacketizer packetizer = new AACADTSPacketizer(); 
		this.packetizer = packetizer;
		
	}

	public void prepare() throws IllegalStateException, IOException {
		
		if (!aacSupported) {
			throw new IllegalStateException("AAC not supported !");
		}
		
		setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
		
		// This is completely experimental: AAC_ADTS is not yet visible in the android developer documentation
		// Recording AAC ADTS works on my galaxy SII with this tiny trick
		setOutputFormat(6);
		setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
		setAudioChannels(1);
		setAudioSamplingRate(8000);
		
		super.prepare();
	}
	
	/* streamtype ?
	 * profile-level-id ?
	 * config ?
	 * Profile ?
	 */
	
	public String generateSessionDescriptor() {
		return "m=audio "+String.valueOf(getDestinationPort())+" RTP/AVP 96\r\n" +
				"b=RR:0\r\n" +
				"a=rtpmap:96 mpeg4-generic/8000\r\n" +
				"a=fmtp:96 streamtype=5; profile-level-id=15; mode=AAC-hbr; config=1588; SizeLength=13; IndexLength=3; IndexDeltaLength=3; Profile=1;\r\n";
	}
	
}
